package cn.edu.stu.syllabus.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.datas.ThemeStyle;
import cn.edu.stu.syllabus.switcher.DayNightSwitcher;
import cn.edu.stu.syllabus.utils.DensityUtil;
import cn.edu.stu.syllabus.utils.ProgressBarColorTintUtils;

/**
 * Created by daidaijie on 2017/3/5.
 */

public abstract class BaseFragment extends Fragment {

    private static final String TAG = "BaseFragment";

    @Nullable
    @BindView(R.id.swipeRefreshLayout)
    protected SwipeRefreshLayout mSwipeRefreshLayout;
    @Nullable
    @BindView(R.id.progressBar)
    protected ProgressBar mProgressBar;
    @Nullable
    @BindView(R.id.topView)
    protected View mTopView;

    //Fragment的View加载完毕的标记
    private boolean isViewCreated;

    //Fragment对用户可见的标记
    private boolean isUIVisible;

    protected BaseActivity mActivity;

    protected int deviceWidth;
    protected int deviceHeight;

    private AppBarShowHideListener mAppBarShowHideListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (BaseActivity) getActivity();
        deviceWidth = mActivity.getWindowManager().getDefaultDisplay().getWidth();
        deviceHeight = mActivity.getWindowManager().getDefaultDisplay().getHeight();
        mAppBarShowHideListener = mActivity;

        Log.i(TAG, "initSize: " + deviceHeight);
        Bundle args = getArguments();
        if (args != null) {
            initArguments(getArguments());
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getContentViewId(), container, false);
        ButterKnife.bind(this, view);

        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setColorSchemeResources(ThemeStyle.getCurrentColorPrimaryLight());
            if (DayNightSwitcher.getInstance().isNightMode()) {
                mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(
                        ContextCompat.getColor(SyllabusApplication.getContext(), R.color.colorCardPress)
                );
            }
        }
        if (mProgressBar != null) {
            ProgressBarColorTintUtils.setColor(mProgressBar, ThemeStyle.getCurrentColorPrimaryLight());
        }

        if (mTopView != null) {
            initTopView();
        }

        init(savedInstanceState);
        return view;
    }

    private void initTopView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewGroup.LayoutParams layoutParams = mTopView.getLayoutParams();
            layoutParams.height = layoutParams.height + mActivity.getStatusBarHeight() - DensityUtil.dip2px(4);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isViewCreated = true;

        lazyLoad();
    }


    @LayoutRes
    protected abstract int getContentViewId();

    protected abstract void init(Bundle savedInstanceState);

    // 空实现，需要的时候才进行继承
    // 在arguments不为空时对其进行处理
    protected void initArguments(Bundle arguments) {

    }

    // 空实现，需要的时候才进行继承
    protected void loadData() {
    }

    private void lazyLoad() {
        // 这里进行双重标记判断,是因为setUserVisibleHint会多次回调,并且会在onCreateView执行前回调,必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUIVisible) {
            loadData();
            // 数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    protected void showAppBar() {
        if (mAppBarShowHideListener != null) {
            mAppBarShowHideListener.onShow();
        }
    }

    protected void hideAppBar() {
        if (mAppBarShowHideListener != null) {
            mAppBarShowHideListener.onHide();
        }
    }
}
